import com.mt.components.consts.Exception;
import com.mt.components.consts.PermissionName;
import com.mt.components.consts.SysConst;
import com.mt.components.utils.AlertFactory;
import com.mt.components.utils.RemoteFactory;
import com.mt.system.domain.Permission;
import com.mt.system.domain.Role;

import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.core.FlexGlobals;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;

/**      
 * 文件名称：SaveRolePermissionAs.as<br>
 * 文件描述：<br>
 * 创建人：马腾<br>
 * 创建时间：Apr 3, 2013 2:34:40 PM<br>
 * 修改人：马腾<br>
 * 修改时间：Apr 3, 2013 2:34:40 PM<br>
 */


public var role:Role;
[Bindable]private var permissions:ArrayCollection;

override protected function init(event:FlexEvent):void{
	super.init(event);
	getRoleTreeData();
	if(role){
		nameText.text = role.name;
	}
}

private function getRoleTreeData():void {
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("PermissionService");
	remote.getPermissionTreeByUser(FlexGlobals.topLevelApplication.user , role);
	remote.addEventListener(ResultEvent.RESULT , resultPermission);
	remote.addEventListener(FaultEvent.FAULT , function (e:FaultEvent):void {
		if(e.message.toString().indexOf(Exception.OPERATE_FAILURE) != -1){
			AlertFactory.getInstance().alert("当前用户的权限小于该角色的权限，不能对其进行修改！");
		}
		closeCurrentPage(null , false);
	});
}

protected function resultPermission(event:ResultEvent):void {
	permissions = event.result as ArrayCollection;
	permissionTree.callLater(expandTree);
}

/**
 * 展开树节点 
 */
private function expandTree():void {
	for each(var permission:Permission in permissionTree.dataProvider){
		permissionTree.expandChildrenOf(permission , true);
	}
}

/**
 * 保存角色权限 
 */
protected function save():void {
	var selectedPermissions:ArrayCollection = new ArrayCollection();
	for each(var permission:Permission in permissionTree.dataProvider){
		if(permission.selected){
			selectedPermissions.addItem(permission);
			setChidlren(permission , selectedPermissions);
		}
	}
	if(selectedPermissions.length == 0){
		mes.text = "请输入角色的权限！";return;
	}
	var temp:Role = new Role();
	temp.name = role.name;
	temp.permissions = selectedPermissions;
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("RoleService" , PermissionName.ROLE_ASSIGN_PER);
	remote.addEventListener(ResultEvent.RESULT , closeCurrentPage);
	remote.updateRolePermission(temp);
}

protected function closeCurrentPage(event:Event , isSuccess:Boolean = true):void {
	if(isSuccess){
		this.dispatchEvent(new Event(SysConst.SAVE_SUCCESS));
	}
	super.close();
}

private function setChidlren(parent:Permission, selectedPermissions:ArrayCollection):void {
	if(parent.children){
		for each(var child:Permission in parent.children){
			if(child.selected){
				selectedPermissions.addItem(child);
				setChidlren(child , selectedPermissions);
			}
		}
	}
}
